package com.dudu.access;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import EcpOpen.Model.SimpleReference;
import EcpOpen.constant.Constants;
import EcpOpen.http.Extend.CCSM.createConference;
import EcpOpen.http.Extend.CCSM.disconnectParticipants;
import EcpOpen.http.Extend.CCSM.endConference;
import EcpOpen.http.Extend.CCSM.inviteParticipants;
import EcpOpen.http.Extend.CCSM.muteParticipants;
import EcpOpen.http.Extend.CCSM.restoreParticipants;
import EcpOpen.http.Extend.MCSM.sendSms;
import EcpOpen.http.Extend.VCSM.endCall;
import EcpOpen.http.Extend.VCSM.makeCall;

import com.dudu.bean.ResultEcpCreateConference;
import com.dudu.utils.ProperyResource;

/**
 * ECP 通道Utils
 * 
 * @author
 * 
 */
public class EcpUtils {

    // 设置Constants中的常量值，web工程中应保证这些值能持久化
    static {
	Constants.extendUrl = "http://open.ecplive.cn:8080" + "/EcpOpen/services/ExtendService/extendService";
	Constants.spid = "0902301640003";
	Constants.serviceKey = "f045bb9105594a53a4bbf24843c17906";
	Constants.appid = "01155791";
	Constants.appkey = "af09b57d6e7b45cbb8620d76188ac126";
	Constants.type = "1";
    }

    /**
     * 点击拨号
     * 
     * @param caller
     *            主叫
     * @param callee
     *            被叫号码
     * @param account
     *            ECP号码（主显号）
     * @return
     */
    public static String makeVoiceCall(String caller, String callee, String account) {
	String result = null;
	try {
	    // 实例化makeCall接口类
	    makeCall makeCallExam = new makeCall();
	    makeCallExam.init();
	    // 接收通话状态返回报告的地址
	    SimpleReference simpleRef = new SimpleReference();
	    simpleRef.setEndpoint(ProperyResource.getEcp_Status_Url());
	    simpleRef.setInterfaceName("notifyCallInformation");
	    simpleRef.setCorrelator("");
	    // 添加必要参数，详见各接口API文档
	    makeCallExam.setAccount(account);// 17005881234
	    makeCallExam.setCallingParty(caller);
	    makeCallExam.setCalledParty(callee);
	    makeCallExam.setStatusRequest(simpleRef);
	    String response = makeCallExam.invokeClient(true);
	    // 解析返回值
	    Document doc = DocumentHelper.parseText(response);
	    Element root = doc.getRootElement();
	    result = root.elementTextTrim("CallIdentifier");
	    /*
	     * 获取属性 String result = root.elementTextTrim("result"); String
	     * callIdentifier = root.elementTextTrim("CallIdentifier"); String
	     * sessionToken = root.elementTextTrim("SessionToken");
	     */
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return result;
    }

    /**
     * 结束呼叫
     * 
     * @param identifier
     *            通话标识
     * @return
     */
    public static String endCall(String callidentifier) {
	String result = null;
	try {
	    // 实例化endCall接口类
	    endCall endCallExam = new endCall();
	    endCallExam.init();
	    // 添加必要参数，详见各接口API文档
	    endCallExam.setCallIdentifier(callidentifier);
	    // 调用invokeClient方法发起调用请求
	    String response = endCallExam.invokeClient();
	    // 解析返回值
	    Document doc = DocumentHelper.parseText(response);
	    Element root = doc.getRootElement();
	    result = root.elementTextTrim("CallIdentifier");
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return result;
    }

    /**
     * 发送短信
     * 
     * @param phone
     *            接收短信号码
     * @param content
     *            短信内容
     * @param account
     *            ECP号码（主显号）
     * @return
     */
    public static String sendMsg(String phone, String content, String account) {
	try {
	    // 短信发送状态接收地址
	    SimpleReference simpleRef = new SimpleReference();
	    simpleRef.setEndpoint("http://117.27.234.109:8180/test/recsms.do");
	    simpleRef.setInterfaceName("recsms");
	    simpleRef.setCorrelator("");
	    // 实例化sendSms接口类
	    sendSms sendSmsExam = new sendSms();

	    sendSmsExam.init();
	    // 添加必要参数，详见各接口API文档
	    sendSmsExam.setAccount(account);// 05723333333
	    sendSmsExam.setDestinationAddresses(phone);
	    sendSmsExam.setMessage(content);
	    sendSmsExam.setReceiptRequest(simpleRef);
	    // 调用invokeClient方法发起调用请求
	    String response = sendSmsExam.invokeClient(true);
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * 创建会议
     * 
     * @param account
     *            ecp号（主显号）
     * @return
     */
    public static String createConference(String account) {
	try {
	    // 回调地址
	    SimpleReference simpleRef = new SimpleReference();
	    simpleRef.setEndpoint(ProperyResource.getEcp_Status_Url());
	    simpleRef.setInterfaceName("notifyCallInformation");
	    simpleRef.setCorrelator("");
	    // 实例化createConference接口类
	    createConference createConferenceExam = new createConference();
	    createConferenceExam.init();
	    // 添加必要参数，详见各接口API文档
	    createConferenceExam.setAccount(account);// 057181111111 17005881234
						     // 18910888275 17005881234
	    createConferenceExam.setStartTime("");
	    createConferenceExam.setMaxDuration(100);
	    createConferenceExam.setMasterPwd("123456");
	    createConferenceExam.setConferencePwd("1");
	    createConferenceExam.setMaxParticipants(10);
	    createConferenceExam.setJoinAllowed(true);
	    createConferenceExam.setStatusRequest(simpleRef);

	    // 调用invokeClient方法发起调用请求
	    String response = createConferenceExam.invokeClient(true);

	    /*
	     * Document doc = DocumentHelper.parseText(response); Element root =
	     * doc.getRootElement(); String result =
	     * root.elementTextTrim("result"); String conferenceIdentifier =
	     * root.elementTextTrim("ConferenceIdentifier"); String SessionToken
	     * = root.elementTextTrim("SessionToken");
	     * 
	     * if(result.equals("1100")){
	     * inviteNumber(conferenceIdentifier,phones); }
	     */

	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    public static ResultEcpCreateConference createEcpConference(String account, String phones) {

	ResultEcpCreateConference resultEcpCreateConference = new ResultEcpCreateConference();

	try {
	    // 回调地址
	    SimpleReference simpleRef = new SimpleReference();
	    simpleRef.setEndpoint(ProperyResource.getEcp_Status_Url());
	    simpleRef.setInterfaceName("notifyCallInformation");
	    simpleRef.setCorrelator("");
	    // 实例化createConference接口类
	    createConference createConferenceExam = new createConference();
	    createConferenceExam.init();
	    // 添加必要参数，详见各接口API文档
	    createConferenceExam.setAccount(account);// 057181111111 17005881234
						     // 18910888275 17005881234
	    createConferenceExam.setStartTime("");
	    createConferenceExam.setMaxDuration(100);
	    createConferenceExam.setMasterPwd("123456");
	    createConferenceExam.setConferencePwd("1");
	    createConferenceExam.setMaxParticipants(10);
	    createConferenceExam.setJoinAllowed(true);
	    createConferenceExam.setStatusRequest(simpleRef);

	    // 调用invokeClient方法发起调用请求
	    String response = createConferenceExam.invokeClient(true);

	    Document doc = DocumentHelper.parseText(response);
	    Element root = doc.getRootElement();
	    String result = root.elementTextTrim("result");
	    String conferenceIdentifier = root.elementTextTrim("ConferenceIdentifier");
	    // String SessionToken = root.elementTextTrim("SessionToken");

	    resultEcpCreateConference.setResult(result);
	    resultEcpCreateConference.setConferenceIdentifier(conferenceIdentifier);

	    if (result.equals("1100")) {
		inviteNumber(conferenceIdentifier, phones);
	    }

	    return resultEcpCreateConference;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * 邀请与会者
     * 
     * @param conferenceIdentifier
     *            会议标识
     * @param numbers
     *            与会者号码
     * @return
     */
    public static String inviteNumber(String conferenceIdentifier, String numbers) {
	try {
	    // 实例化inviteParticipants接口类
	    inviteParticipants inviteParticipantsExam = new inviteParticipants();
	    inviteParticipantsExam.init();
	    // 添加必要参数，详见各接口API文档
	    inviteParticipantsExam.setConferenceIdentifier(conferenceIdentifier);
	    String[] pa = { numbers };
	    inviteParticipantsExam.setParticipants(pa);
	    // 调用invokeClient方法发起调用请求
	    String response = inviteParticipantsExam.invokeClient();
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * 断开与会者
     * 
     * @param conferenceIdentifier
     * @param numbers
     * @return
     */
    public static String disconnectParticipants(String conferenceIdentifier, String numbers) {
	try {
	    // 实例化disconnectParticipants接口类
	    disconnectParticipants disconnectParticipantsExam = new disconnectParticipants();
	    disconnectParticipantsExam.init();
	    // 添加必要参数，详见各接口API文档
	    disconnectParticipantsExam.setConferenceIdentifier(conferenceIdentifier);
	    String[] pa = { numbers };
	    disconnectParticipantsExam.setParticipants(pa);
	    // 调用invokeClient方法发起调用请求
	    String response = disconnectParticipantsExam.invokeClient();
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * 静音与会者
     * 
     * @param conferenceIdentifier
     * @param numbers
     * @return
     */
    public static String muteParticipants(String conferenceIdentifier, String numbers) {
	try {
	    // 实例化muteParticipants接口类
	    muteParticipants muteParticipantsExam = new muteParticipants();
	    muteParticipantsExam.init();
	    // 添加必要参数，详见各接口API文档
	    muteParticipantsExam.setConferenceIdentifier(conferenceIdentifier);
	    String[] pa = { numbers };
	    muteParticipantsExam.setParticipants(pa);
	    // 调用invokeClient方法发起调用请求
	    String response = muteParticipantsExam.invokeClient();
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}

    }

    /**
     * 恢复静音与会者
     * 
     * @param conferenceIdentifier
     * @param numbers
     * @return
     */
    public static String restoreParticipants(String conferenceIdentifier, String numbers) {
	try {
	    // 实例化sendSms接口类
	    restoreParticipants restoreParticipantsExam = new restoreParticipants();
	    restoreParticipantsExam.init();
	    // 添加必要参数，详见各接口API文档
	    restoreParticipantsExam.setConferenceIdentifier(conferenceIdentifier);
	    String[] pa = { numbers };
	    restoreParticipantsExam.setParticipants(pa);
	    // 调用invokeClient方法发起调用请求
	    String response = restoreParticipantsExam.invokeClient();
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}

    }

    /**
     * 结束会议
     * 
     * @param conferenceIdentifier
     * @return
     */
    public static String endConference(String conferenceIdentifier) {
	try {
	    // 实例化endConference接口类
	    endConference endConferenceExam = new endConference();
	    endConferenceExam.init();
	    // 添加必要参数，详见各接口API文档
	    endConferenceExam.setConferenceIdentifier(conferenceIdentifier);
	    // 调用invokeClient方法发起调用请求
	    String response = endConferenceExam.invokeClient();
	    return response;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    public static void main(String[] args) {
	makeVoiceCall("17001090136", "17001097473", "17001097473");
	// endCall("201508271028151271372787");
	// sendMsg("18662187762", "ecp短信通道测试", "17001090102");
    }

}
